package com.littlepay.kr4.paulin.dfeba77.application;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.littlepay.kr4.paulin.dfeba77.remind.MyReceiver;
import com.littlepay.kr4.paulin.dfeba77.utils.PrefUtils;

import java.util.Calendar;

/**
 * Created by john on 2017/5/15.
 */

public class MyApplication extends Application {
    private static MyApplication app;
    private Context mContext;
    private int budget;
    private long currentID;
    private boolean islogin;
    private boolean isremind;
    private boolean isAutoSync;

    public boolean isContinuousSticker() {
        return isContinuousSticker;
    }

    public void setContinuousSticker(boolean continuousSticker) {
        isContinuousSticker = continuousSticker;
    }

    private boolean isContinuousSticker;
    //private int remainmoney;
    private boolean welcome;
    //PrefUtils pref;

    public int getBudget() {
        return budget;
    }
    public void setBudget(String s)
    {
        budget = Integer.parseInt(s);
    }
    public void setWifiSync(boolean x)
    {
        isAutoSync = x;
    }
    
    public void setIsremind(boolean x)
    {
        isremind = x;
    }
    public long getCurrentID() {
        return currentID;
    }

    public boolean isremind() {
        return isremind;
    }

    public boolean isWifiSync() {
        return isAutoSync;
    }



    public boolean isWelcome() {
        return welcome;
    }

    public Context getContext() {
        return mContext;
    }
    public void setReminder(boolean b) {

        // get the AlarmManager instance
        AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);
        // create a PendingIntent that will perform a broadcast
        PendingIntent pi= PendingIntent.getBroadcast(this, 0, new Intent(this,MyReceiver.class), 0);

        Calendar d = Calendar.getInstance();
        d.setTimeInMillis(System.currentTimeMillis());
        d.set(Calendar.AM_PM,1);
        d.set(Calendar.HOUR,7);
        d.set(Calendar.MINUTE,00);
        Calendar e = Calendar.getInstance();
        e.setTimeInMillis(System.currentTimeMillis());
        if(e.getTimeInMillis()>d.getTimeInMillis())
            d.set(Calendar.DAY_OF_YEAR,d.get(Calendar.DAY_OF_YEAR)+1);
//        Calendar e = null;
//        e.set(Calendar.DAY_OF_YEAR,d.get(Calendar.DAY_OF_YEAR));
//        e.set(Calendar.HOUR_OF_DAY,13);
//        e.set(Calendar.MINUTE,52);
//        if(d.getTimeInMillis()>e.getTimeInMillis())
//            e.set(Calendar.DAY_OF_YEAR,d.get(Calendar.DAY_OF_YEAR)+1);
//        d.set(Calendar.DATE,)
        //d.set(Calendar.PM,2);
       //d.set(Calendar.HOUR, 2);//每天下午7点准时提醒
       //d.set(Calendar.MINUTE, 13);
        if(b){
            // just use current time as the Alarm time.
            //Calendar c=Calendar.getInstance();
            // schedule an alarm
            //am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
            am.set(AlarmManager.RTC_WAKEUP, d.getTimeInMillis(), pi);
            System.out.println(e);

        }
        else{
            // cancel current alarm
            am.cancel(pi);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        mContext = this;
        welcome = PrefUtils.getwelcome();
        if(welcome)
        {
            PrefUtils.setcurrentID("0");
            PrefUtils.setLogin(false);
            PrefUtils.setBudget("0");
            //PrefUtils.setremainmoney(0);
            PrefUtils.setwifi_sync(false);
            PrefUtils.setContinuousSticker(false);
            PrefUtils.remindMode(false);
            //PrefUtils.setwelcome(false);
            PrefUtils.setRename("请输入昵称");
        }

        //pref = new PrefUtils();
        //判断是否首次登录程序

        budget = Integer.parseInt(PrefUtils.getBudget());
        islogin = PrefUtils.getLogin();
        isremind = PrefUtils.isRemindMode();
        isAutoSync = PrefUtils.iswifi_sync();
        isContinuousSticker = PrefUtils.getContinuousSticker();
        PrefUtils.initcurrentID();
        currentID = Long.parseLong(PrefUtils.getcurrentID());
        //remainmoney = PrefUtils.getremainmoney();
    }

    public void onTerminate() {
        super.onTerminate();
        //Fresco.shutDown();
        //ActiveAndroid.dispose();
    }

    public static MyApplication getInstance(){
        return app;
    }

    public void refresh()
    {
        budget = Integer.parseInt(PrefUtils.getBudget());
        islogin = PrefUtils.getLogin();
        isremind = PrefUtils.isRemindMode();
        isAutoSync = PrefUtils.iswifi_sync();
        isContinuousSticker = PrefUtils.getContinuousSticker();
        currentID = Long.parseLong(PrefUtils.getcurrentID());
    }

}
